package com.jron.cqiu.util;

import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.commons.collections.map.HashedMap;

import java.util.Map;

/**
 * Created by Intellij IDEA.
 *
 * @Author: Yejw
 * @Date: 2008-6-20
 * @Time: 10:05:40
 */
public interface Message {

    /**
     * 格式化消息
     * 如:
     * <p/>
     * FORMAT.format("CREATE TABLE ${tab} (${cid},${ctime})",
     * new String[]{"tab",cTable}, new String[]{"cid",cId}, new String[]{"ctime",cTime})
     * <p/>
     * 或者:
     * <p/>
     * FORMAT.format("CREATE TABLE ${0} (${1},${2})", cTable, cId, cTime)
     * <p/>
     * 或者:
     * <p/>
     * Map m = new HashedMap();
     * m.put("tab", cTable);
     * m.put("cid", cId);
     * m.put("ctime", cTime);
     * FORMAT.format("CREATE TABLE ${tab} (${cid},${ctime})", m);
     */
    public static final IFormat FORMAT = new IFormat() {
        public String format(String source, Object... args) {
            Map m = new HashedMap();
            int i = 0;
            for (Object arg : args) {
                if (args.length == 1 && (arg instanceof Map)) {
                    m = (Map) arg;
                    break;
                } else if (arg instanceof Object[]) {
                    Object[] s = (Object[]) arg;
                    m.put(s[0], s[1]);
                } else {
                    m.put(String.valueOf(i), arg);
                    i++;
                }
            }
            return StrSubstitutor.replace(source, m);
        }
    };

    /** 不合法邮件地址验证信息 */
    public static final IMessage invalid_email_msg = new IMessage() {
        public String message(Object... args) {
            return String.format("%s is not valid e-mail!", args[0]);
        }
    };

}
